<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_page_content_filter;
use cmf\lib\Plugin;
use think\Config;
use think\db;
use think\Request;

class QbtPageContentFilterPlugin extends Plugin{

    //插件的配置信息
    public $info = array(
        'name'=>'QbtPageContentFilter',
        'title'=>'页面渲染处理插件',
        'description'=>'页面渲染处理插件',
        'status'=>1,
        'author'=>'大楠',
        'version'=>'1.0'
    );

    public $hasAdmin = 1;

    //安装方法必须实现
    public function install()
    {
        $config = Config::get('database');
        Db::query("
            CREATE TABLE IF NOT EXISTS `{$config['prefix']}view_filter` (
            `id`  int NOT NULL AUTO_INCREMENT,  
            `rules`  varchar(255) NULL DEFAULT '' COMMENT '规则一般是正则' ,
            `url`  varchar(255) NULL COMMENT '控制器方法' ,
            `content`  varchar(255) NULL DEFAULT '' COMMENT '替换的内容' ,
            `status`  tinyint(2) NULL COMMENT '1为启用2禁用' ,
            `addtime`  int NULL COMMENT '添加时间' ,
            PRIMARY KEY (`id`)
            )
        ");
        return true;
    }

    //卸载方法必须实现
    public function uninstall()
    {
        $config = Config::get('database');
        Db::query("DROP TABLE `{$config['prefix']}view_filter`");
        return true;
    }


    public function run(){

    }

    public function viewFilter(&$content){
        $config = Config::get('database');
        $action = request()->action();
        $controller = request()->controller();
        $module = request()->module();
        $url = $module.'/'.$controller.'/'.$action;
        $path = request()->path();
        $new_path = explode('/',$path);
        $last_path = $new_path[0].'/';
        unset($new_path[0]);

        $status = Db::query("SELECT table_name FROM information_schema.TABLES WHERE table_name ='{$config['prefix']}view_filter';");
        if(!empty($status)){
            $list =  Db::name('view_filter')->where('status = 1')->select()->toArray();
            $where['url'] = array('like',"%$last_path%");
            $new_list =  Db::name('view_filter')->where($where)->where('status =1')->select()->toArray();
            $neww_list = [];
            foreach($new_list as $k=>$v){
                $new_v = explode('/',$v['url']);
                $count = count($new_v);
                unset($new_v[0]);

                if($new_v[1] == $new_path[1] || $new_v[1] == '*'){
                    if($new_v[2] == $new_path[2] || $new_v[2] == '*'){
                        if($count == 4){
                            if($new_v[3] == $new_path[3] || $new_v[3] == '*'){
                                $neww_list[$k] = $v;
                            }
                        }else{
                            $neww_list[$k] = $v;
                        }
                    }
                }
            }

            foreach($neww_list as $k=>$v){
                    $content = preg_replace(htmlspecialchars_decode($v['rules']),htmlspecialchars_decode($v['content']),$content);
            }
            foreach($list as $k=>$v){
                if($v['url'] == '*' && strpos($path,'plugin/qbt_page_content_filter/') === false){
                    $content = preg_replace(htmlspecialchars_decode($v['rules']),htmlspecialchars_decode($v['content']),$content);
                }
            }

            return $content;
        }
    }
}